home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / thesource6.dms / thesource6.adf / Source / StartUpCode / StartUpCode.lha / StartUp.s < prev   
Encoding:
Text File  |  1993-05-20  |  7.5 KB  |  249 lines

  1. ***********************************************************
  2. * $VER: StartUp_S 4.00
  3. *
  4. * This is my TakeSystem and RestoreSystem routines that I now use
  5. * in all my demos.  I am spreading them in hopes to get rid of the
  6. * crapy coding practices that people have had since the C64 days.
  7. * I'd like to thank Comrade J for his excellent "howtocode" files.
  8. * A lot of the stuff here is based on his words of wisdom. :^).  Anyway,
  9. * these routines, as of version 4.00, handle the following:
  10. *        Getting the VBR.
  11. *        Saving DMACON.
  12. *        Saving Intena.
  13. *        Saving and reseting WB view modes.
  14. *        Owning/Disowning the blitter.
  15. *        Pal/Ntsc/AGA modes.
  16. *        Killing multitasking.
  17. *        Killing interrupts.
  18. *        Fixed equates for case sensitive assemblers.
  19. *        PC relative.
  20. *        Fully linkable code.
  21. *        Added more conditional assembly for un-needed code.
  22. *
  23. * Feel free to cut and paste to fit your code (i.e. put the data and bss in
  24. * your data and bss sections.), but please leave the CODE intact!
  25. * If you have any questions/comments/suggestions, PLEASE contact me at
  26. * either:
  27. *        idr@rigel.cs.pdx.edu        (internet)
  28. *    - or -
  29. *        Epsilon
  30. *        P.O.B.    1914
  31. *        Beaverton, OR  97075-1914
  32. *        U.S.A.
  33. *
  34. * Many of you may not like the fact that I use includes and system calls,
  35. * I know I don't :), but it is the ONLY SAFE WAY TO TAKE THE SYSTEM!  To
  36. * insure that your demo will work on any system, it's what you have to do.
  37. *
  38. ***********************************************************
  39. * Includes
  40. ***********************************************************
  41.  
  42.         incdir    "Devel:include/"
  43.         include    "exec/exec_lib.i"
  44.         include    "exec/execbase.i"
  45.         include    "graphics/gfxbase.i"
  46.         include    "graphics/graphics_lib.i"
  47.         include    "hardware/custom.i"
  48.  
  49. ***********************************************************
  50. * Other Macros
  51. ***********************************************************
  52.  
  53. CALL        MACRO
  54.         jsr    _LVO\1(a6)
  55.         ENDM
  56.  
  57. ***********************************************************
  58. * Assembly Flags
  59. ***********************************************************
  60.  
  61. ; These items can be optional...that is, they may not apply to your
  62. ; code.
  63. CHECK_FOR_PAL    SET    1        ; set to 1 if you need to know if PAL
  64. ; If you do not intend to use sprites in your code, make this 0.
  65. CHECK_FOR_AGA    SET    1        ; set to 1 if you need to know if AGA
  66. ; If you (and your mod player!) will not use interrupts, set this to 0.
  67. CHECK_FOR_VBR    SET    1        ; set to 1 if you need to know what
  68.                     ;   the VBR is.
  69. MAKE_LINKABLE    SET    1        ; if you want linkable code.
  70.  
  71. ***********************************************************
  72. * Other Structs
  73. ***********************************************************
  74.  
  75.         rsreset
  76. gfx_base    rs.l    1        ; pointer to graphics base
  77. OldView        rs.l    1        ; old Work Bench view addr.
  78. OldDMACon:    rs.w    1        ; old dmacon bits
  79. OldINTEna:    rs.w    1        ; old intena bits
  80. VectorBase:    rs.l    1        ; pointer to the Vector Base
  81. ntsc:        rs.b    1        ; 0 = pal, 1 = ntsc
  82. AGA:        rs.b    1        ; 0 = ESC/OCS, 1 = AGA
  83. SYS_SIZE    rs.b    0        ; size of the struct
  84.  
  85. ***********************************************************
  86. * Other Equates
  87. ***********************************************************
  88.  
  89. ; This is from the 3.0 includes which almost nobody has.
  90.  IFND    GFXB_AA_ALICE
  91. GFXB_AA_ALICE    SET    2
  92.  ENDC
  93.  
  94. ; These are from the 2.0 includes which many people still don't have.
  95.  
  96.  IFND    gb_ChipRevBits0
  97. gb_ChipRevBits0    SET    $ec
  98.  ENDC
  99.  
  100.  IFND    PALn
  101. PALn        SET    2
  102.  ENDC
  103.  
  104. ***********************************************************
  105.  
  106.  IFNE    MAKE_LINKABLE
  107.         xdef    TakeSystem    ; call to shut system down
  108.         xdef    RestoreSystem    ; call to bring system back
  109.  ENDC
  110.  
  111.         section TheCode,code
  112.  
  113. ***********************************************************
  114. * Inputs:  a4 : points to SystemSave
  115. * Outputs: SystemSave is filled with correct data
  116. *          a5 : $dff002
  117. *          d0 : zero if success, or -1 if failure
  118.  
  119. TakeSystem:    movea.l    4.w,a6        ; exec base
  120.         lea    $dff002,a5    ; custom chip base + 2
  121.  
  122.         lea    GraphicsName(pc),a1    ; "graphics.library"
  123.         moveq    #33,d0        ; any version that is at least 1.2 :)
  124.         CALL    OpenLibrary    ; open it.
  125.         move.l    d0,gfx_base(a4)    ; save pointer to gfx base
  126.         beq.w    .erexit        ; if we got a NULL, then exit
  127.         movea.l    d0,a6        ; for later callls...
  128.  
  129.         move.l  gb_ActiView(a6),OldView(a4)    ; save old view
  130.  
  131.         move.w    #0,a1        ; clears full long-word
  132.         CALL    LoadView    ; Open a NULL view (resets display
  133.                     ;   on any Amiga)
  134.  
  135.         CALL    WaitTOF        ; Wait twice so that an interlace
  136.         CALL    WaitTOF        ;   display can reset.
  137.  
  138.         CALL    OwnBlitter    ; take over the blitter and...
  139.         CALL    WaitBlit    ;   wait for it to finish so we
  140.                     ;   safely use it as we please.
  141.  
  142.         movea.l    4.w,a6        ; exec base
  143.         CALL    Forbid        ; kill multitasking
  144.  
  145.  IFNE    CHECK_FOR_PAL
  146.         cmpi.l    #37,LIB_VERSION(a6)    ; 2.04 or better?
  147.         bge.b    .os20        ; yup
  148.         cmpi.b    #50,VBlankFrequency(a6)    ; is vblank rate pal?
  149.         beq.b    .pal        ; yup.
  150.         st    ntsc(a4)    ; set NTSC flag.
  151.         bra.b    .pal
  152.  
  153. .os20:        movea.l    gfx_base(a4),a0    ; graphics base
  154.         btst.b    #PALn,gb_DisplayFlags(a0)
  155.         bne.b    .pal        ; PAL display mode?
  156.         st    ntsc(a4)    ; set NTSC flag.
  157. .pal:
  158.  ENDC
  159.  
  160.  IFNE    CHECK_FOR_VBR
  161.         moveq    #0,d0            ; clear
  162.         btst.b    #AFB_68010,AttnFlags+1(a6); are we at least a 68010?
  163.         beq.b    .just_68000        ; nope.
  164.         move.l    a5,-(sp)        ; save it.
  165.         lea.l    vbr_exception(pc),a5    ; addr of function to get VBR
  166.         CALL    Supervisor        ; supervisor state
  167.         move.l    (sp)+,a5        ; restore it.
  168. .just_68000:    move.l    d0,VectorBase(a4)    ; save it for later.
  169.  ENDC
  170.  
  171.  IFNE    CHECK_FOR_AGA
  172. ; Take a look at HowToCode 5 or 6 to see why this is the correct
  173. ; way to take care of this.
  174.         movea.l    gfx_base(a4),a0
  175.         btst.b    #GFXB_AA_ALICE,gb_ChipRevBits0(a0)
  176.         beq.b    .not_aga    ; nope.
  177.         st    AGA(a4)        ; set the AGA flag.
  178.         moveq    #0,d0
  179.         move.w    d0,$1fc-2(a5)    ; reset AGA sprites to normal mode
  180.         move.w    d0,$106-2(a5)    ; reset AGA sprites to normal mode
  181. .not_aga:
  182.  ENDC
  183.  
  184.         move.w    dmaconr-2(a5),d0    ; old DMACON bits
  185.         ori.w    #$8000,d0    ; or it set bit for restore
  186.         move.w    d0,OldDMACon(a4); save it
  187.  
  188.         move.w    intenar-2(a5),d0    ; old INTEna bits
  189.         ori.w    #$c000,d0    ; or it set bit for restore
  190.         move.w    d0,OldINTEna(a4); save it
  191.  
  192.         move.l    #$7fff7fff,intena-2(a5)    ; kill all ints
  193.         move.w    #$7fff,dmacon-2(a5)    ; kill all dma
  194.         moveq    #0,d0        ; return no error code
  195.         rts
  196.  
  197. .erexit:    moveq    #-1,d0        ; error, don't run demo.
  198.         rts
  199.  
  200. ***********************************************************
  201.  
  202.  IFNE    CHECK_FOR_VBR
  203. vbr_exception:
  204.     ; movec vbr,Xn is a priv. instr.  You must be supervisor to execute!
  205.         movec   vbr,d0
  206.     ; many assemblers don't know the VBR, if yours doesn't, then use this
  207.     ; line instead.
  208. ;        dc.l    $4e7a0801
  209.         rte                ; back to user state code
  210.  ENDC
  211.  
  212. ***********************************************************
  213. * Inputs:  a4 : points to SystemSave
  214. * Outputs: d0 : zero (return code)
  215.  
  216. RestoreSystem:    lea    $dff000+dmacon,a5    ; custom chip base + dmacon
  217.  
  218.     ; You must do these in this order or you're asking for trouble!
  219.         move.l    #$7fff7fff,intena-dmacon(a5)    ; kill all ints
  220.         move.w    #$7fff,(a5)            ; kill all dma
  221.         move.w    OldDMACon(a4),(a5)        ; restore old dma bits
  222.         move.w    OldINTEna(a4),intena-dmacon(a5)    ; restore old int bits
  223.  
  224.         movea.l    OldView(a4),a1    ; old Work Bench view
  225.         movea.l    gfx_base(a4),a6    ; gfx base
  226.         CALL    LoadView    ; Restore the view
  227.         CALL    DisownBlitter    ; give blitter back to the system.
  228.  
  229.         move.l    gb_copinit(a6),$80-dmacon(a5) ; restore system clist
  230.         movea.l    a6,a1
  231.         movea.l    4.w,a6        ; exec base
  232.         CALL    CloseLibrary
  233.  
  234.     ; there is no call to Permit() because it is implied by the return
  235.     ; to AmigaDOS! :^)
  236.         moveq    #0,d0
  237.         rts
  238.  
  239. ***********************************************************
  240.  
  241. ; Hey!  This is a constant string!  Why put it in a different segment?
  242. ; With protected mem or re-entrant code this won't cause any problems
  243. ; at all.  Trust me. :) Besides, it makes the code smaller.
  244.  
  245. GraphicsName:    GRAFNAME        ; name of gfx library
  246.         EVEN
  247.  
  248.         END
  249.